/*
	       File: frmUserID.cs
	Create Date: 2007-09-18
	    Version:

	Description:


	 Change log:


*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Text;
using System.Windows.Forms;

namespace GuerillaProgrammer.FileProcessor.ServiceMonitor
{
    public partial class frmUserID : Form
    {
        #region Private member fields
        public const int LOGON32_PROVIDER_DEFAULT = 0;
        public const int LOGON32_LOGON_INTERACTIVE = 2;
        public const int LOGON32_LOGON_SERVICE = 5;
        #endregion

        #region Constructors and cleanup methods
        public frmUserID()
        {
            InitializeComponent();
        }
        #endregion

        #region Private implementation methods
        [DllImport("advapi32.dll")]
        public static extern int LogonUserA(String lpszUserName, String lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

        private bool VerifyUserLogin(string userName, string password, string domain)
        {
            IntPtr token = IntPtr.Zero;

            if (0 != LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        private void SplitupUserName(string userName, ref string parsedUserName, ref string parsedDomain)
        {
            string[] userNameParts = userName.Split(new char[] { '\\' });
            if (2 == userNameParts.Length)
            {
                parsedUserName = userNameParts[1];
                parsedDomain = userNameParts[0];
            }
            else
            {
                parsedDomain = null;
                parsedUserName = userName;
            }
        }
        #endregion

        #region Class properties
        public string UserName
        {
            get 
            {
                if (true == txtUserName.Text.Contains("\\"))
                {
                    return txtUserName.Text;
                }

                return ".\\" + txtUserName.Text;
            }
            set { txtUserName.Text = value; }
        }
        public string Password
        {
            get { return txtPassword.Text; }
            set { txtPassword.Text = value; }
        }
        #endregion

        #region Events handlers
        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }
        private void btnVerify_Click(object sender, EventArgs e)
        {
            string userName = "";
            string domain = "";
            string password = "";

            SplitupUserName(txtUserName.Text, ref userName, ref domain);
            
            password = txtPassword.Text;

            if (false == VerifyUserLogin(userName, password, domain))
            {
                MessageBox.Show("User credentials don't work!\nPlease try again.");
            }
            else
            {
                btnOK.Enabled = true;
            }
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            string userName = "";
            string domain = "";
            string password = "";

            SplitupUserName(txtUserName.Text, ref userName, ref domain);

            password = txtPassword.Text;

            if (false == VerifyUserLogin(userName, password, domain))
            {
                MessageBox.Show("User credentials don't work!\nPlease try again.");
            }
            else
            {
                DialogResult = DialogResult.OK;
                this.Close();
            }
        }
        #endregion

        #region Public interface
        #endregion
    }
}